<?php
    class Product extends MY_Controller
    {
        public function __construct()
        {
            parent::__construct();
            $this->load->model('admin/product_model');
            $this->load->model('admin/category_product_model');
        }
        public function index()
        {
            
           $data=$this->product_model->getAll(10,0,-1);
           $this->load->vars('item',$data);
           
        }
        
        //add new product
        public function add()
        {
            $category=$this->category_product_model->getAll(10,0,1);
            $this->load->vars('category',$category);
        }
        
        //edit product 
        public function edit()
        {
            $id=$this->uri->segment(4);
            $category=$this->category_product_model->getAll(10,0,1);
            $this->load->vars('category',$category);
            $this->data['item']=$this->product_model->getProductById($id);
            $this->load->vars('item',$this->data['item']);
        }
        
        //save new product or update 
        public function save()
        {
            if ($this->form_validation->run() == FALSE)
            {
                $this->form_validation->set_error_delimiters('<div class="error" style="text-align:center;">', '</div>');
                $this->load->view('admin/form/form');
            }
            else
            {
                $post=$this->input->post();
                $id=$post['id'];
                $name=$post['name'];
                $alias=$post['alias'];
                $category=$post['category'];
                $quanlity=$post['quanlity'];
                $price=$post['price'];
                $discount=$post['discount'];
                $property=$post['property'];
                $store=$post['store'];
                $use=$post['use'];
                $description=$post['desciption'];
                $publish=$post['publish'];
                $order=$post['order'];
                $data=array();
                $data['product_name']=$name;
                $data['product_alias']=$alias;
                $data['product_quanlity']=$quanlity;
                $data['product_price']=$price;
                $data['product_discount']=$discount;

                if($id>0)
                {
                    $image=$this->doupload('image','products');
                    $image1=$this->doupload('image1','products');
                    $image2=$this->doupload('image2','products');
                    $image3=$this->doupload('image3','products');
                    $image4=$this->doupload('image4','products');
                    $image5=$this->doupload('image5','products');
                    if($image!="")
                    {
                        $data['product_image_path']=$image;
                    }
                    if($image1!="")
                    {
                        $data['product_image_path1']=$image1;
                    }
                    if($image2!="")
                    {
                        $data['product_image_path2']=$image2;
                    }
                    if($image3!="")
                    {
                        $data['product_image_path3']=$image3;
                    }
                    if($image4!="")
                    {
                        $data['product_image_path4']=$image4;
                    }
                    if($image5!="")
                    {
                        $data['product_image_path5']=$image5;
                    }
                    
                }
                else
                {
                    $image=$this->doupload('image','products');
                    $image1=$this->doupload('image1','products');
                    $image2=$this->doupload('image2','products');
                    $image3=$this->doupload('image3','products');
                    $image4=$this->doupload('image4','products');
                    $image5=$this->doupload('image5','products');
                    $data['product_image_path']=$image;
                    $data['product_image_path1']=$image1;
                    $data['product_image_path2']=$image2;
                    $data['product_image_path3']=$image3;
                    $data['product_image_path4']=$image4;
                    $data['product_image_path5']=$image5;
                }
                $data['product_store']=$store;
                $data['product_description']=$description;
                $data['product_use']=$use;
                $data['product_property']=$property;
                $data['product_category']=$category;
                $data['product_order']=$order;
                $data['product_publish']=$publish;
                //call save
                if($this->product_model->save($id,$data)>0)
                {
                    
                    //redirect(base_url()."admin/product");
                }
                 redirect(base_url()."admin/product"); 
            }// End if formvalidation
          
        }
        //publish product
        public function publish()
        {
            $id=$this->uri->segment(4);
            $this->product_model->publish(1,array($id));
            redirect(base_url()."admin/product");
        }
        //unpublish
        public function unpublish()
        {
            $id=$this->uri->segment(4);
            $this->product_model->publish(0,array($id));
            redirect(base_url()."admin/product");
        }
        //for actions from menu
        
        public function action()
        {
            $task=$this->input->post('task');
            $task=trim($task);
            $id=$this->input->post('id');
            switch($task)
            {
                case 'delete':
                $this->product_model->delete($id);
                break;
                case 'saveorder':
                $cid=$this->input->post('cid');
                $order=$this->input->post('order');
                $this->product_model->saveorder($cid,$order);
                break;
                case 'publish':
                $this->product_model->publish(1,$id);
                break;
                case 'unpublish':
                $this->product_model->publish(0,$id);
                break;
                
            }
            redirect(base_url()."admin/product");
        }
    }
?>